<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="销售订单编号" prop="soNumber">
        <el-input
          v-model="queryParams.soNumber"
          placeholder="请输入销售订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单名称" prop="soName">
        <el-input
          v-model="queryParams.soName"
          placeholder="请输入销售订单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期" prop="params">
        <el-date-picker clearable
                        v-model="queryParams.params"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择订单日期">
        </el-date-picker>
      </el-form-item>
<!--
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="草稿" value="草稿" />
          <el-option label="审批中" value="审批中" />
          <el-option label="已审批" value="已审批" />
          <el-option label="驳回" value="驳回" />
          <el-option label="已完成" value="已完成" />
          <el-option label="已退货" value="已退货" />
        </el-select>
      </el-form-item>
-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes/xs:order:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes/xs:order:edit']"

        >修改</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" border @selection-change="handleSelectionChange">
      <el-table-column label="销售订单编号" align="center" prop="soNumber" width="160px">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes/xs:order:query']"
          >{{scope.row.soNumber}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="销售订单名称" align="center" prop="soName" />
      <el-table-column label="客户编码" align="center" prop="clientCode" />
      <el-table-column label="客户名称" align="center" prop="clientName" :show-overflow-tooltip="true"/>
      <el-table-column label="总金额(元)" align="center" prop="totalAmount">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.totalAmount">{{ formatCurrency(scope.row.totalAmount) }}</el-tag>
          <span v-else>{{ scope.row.totalAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单日期" align="center" prop="orderDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计交货日期" align="center" prop="deliveryDate">
        <template slot-scope="scope">
          <el-tag type="warning">{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="销售人员" align="center" prop="salesPerson" />
      <el-table-column label="所属项目" align="center" prop="productName" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" :show-overflow-tooltip="true"/>
      <el-table-column label="设备方向" align="center" prop="deviceOrientation" :show-overflow-tooltip="true"/>
      <el-table-column label="喷漆要求" align="center" prop="paintingRequirements" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '草稿'" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === '已完成'" type="success">已完成</el-tag>
          <el-tag v-else-if="scope.row.status === '已退货'" type="danger">已退货</el-tag>
          <el-tag v-else-if="scope.row.status === '已审批'" >已审批</el-tag>
          <el-tag v-else-if="scope.row.status === '待审批'" type="info">待审批</el-tag>
          <el-tag v-else-if="scope.row.status === '已驳回'" type="danger">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-tooltip
            content="修改"
            placement="top"
            v-if="scope.row.status === '草稿' || scope.row.status === '已驳回' ">
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['mes/xs:order:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="提交审批"
            placement="top"
            v-if="scope.row.status === '草稿' || scope.row.status === '已驳回' ">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-document-checked"
              @click="handleConfirm(scope.row)"
              v-hasPermi="['mes/xs:order:pass']"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="删除"
            placement="top"
            v-if="scope.row.status === '草稿' || scope.row.status === '已驳回'">
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['mes/xs:order:remove']"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            content="变更"
            placement="top"
            v-if="scope.row.status === '已审批' ">
            <el-button
              size="mini"
              type="success"
              icon="el-icon-magic-stick"
              @click="handleChange(scope.row)"
              v-hasPermi="['mes/xs:order:edit']"
            />
          </el-tooltip>
          <!-- 暂时隐藏 -->
<!--          <el-tooltip
            content="历史记录"
            placement="top"
            v-if="scope.row.status !== '草稿'">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-document"
              @click="handleRecord(scope.row)"
              v-hasPermi="['xs:changeRecord:list']"
            />
          </el-tooltip>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>

    <!-- 添加或修改销售订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-tabs type="border-card" v-model="activeTab" >
        <el-tab-pane  label="订单信息" name="订单信息">
<!--          <div style="font-size: 20px;">订单信息</div>-->
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="销售订单编号" prop="soNumber">
              <el-input
                v-model="form.soNumber"
                placeholder="请输入销售订单编号"
                :readonly="optType !== 'add'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="80">
              <el-switch v-model="autoGenFlag"
                         active-color="#13ce66"
                         active-text="自动生成"
                         @change="handleAutoGenChange(autoGenFlag)"
                         v-if="optType === 'add'">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销售订单名称" prop="soName">
              <el-input
                v-model="form.soName"
                placeholder="请输入销售订单名称"
                :readonly="optType === 'view' || optType === 'change'"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="客户ID" prop="clientId" style="display: none;">
              <el-input v-model="form.clientId" placeholder="请输入客户ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户编码" prop="clientCode">
              <el-input v-model="form.clientCode" placeholder="请输入客户编码" readonly>
                <el-button
                  slot="append"
                  @click="handleSelectClient"
                  icon="el-icon-search"
                  v-if="optType === 'add'"
                >
                </el-button>
              </el-input>
              <ClientSelect ref="clientSelect" @onSelected="onClientSelected" > </ClientSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker clearable
                              v-model="form.orderDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择订单日期"
                              :disabled="optType === 'view'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计交货日期" prop="deliveryDate">
              <el-date-picker clearable
                              v-model="form.deliveryDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择预计交货日期"
                              :disabled="optType === 'view'"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="销售人员" prop="salesPerson">
              <el-input v-model="form.salesPerson" placeholder="请输入销售人员" readonly>
                <el-button
                  slot="append"
                  @click="handleSelectUser"
                  icon="el-icon-search"
                  v-if="optType !== 'view'">
                </el-button>
              </el-input>
              <UserSelect ref="userSelect" @onSelected="onUserSelected" ></UserSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态">
              <el-input v-model="form.status" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="订单部门" prop="deptName">
              <el-input v-model="form.deptName" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单部门ID" prop="deptId" style="display: none;">
              <el-input v-model="form.deptId" readonly />
            </el-form-item>
<!--            <el-form-item label="喷漆要求" prop="paintingRequirements">
              <el-input v-model="form.paintingRequirements" :readonly="optType === 'view'"/>
            </el-form-item>-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属项目" prop="projectCode">
              <el-input v-model="form.productName" placeholder="请选择所属项目" readonly>
                <el-button
                  slot="append"
                  @click="handleSelectProject"
                  icon="el-icon-search"
                  v-if="optType === 'add'"
                >
                </el-button>
              </el-input>
              <ProjectSelect ref="projectSelect" @onSelected="onProjectSelected" > </ProjectSelect>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="deviceCode">
              <el-input v-model="form.deviceCode" :readonly="optType === 'view'"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备方向" prop="deviceOrientation">
              <el-input v-model="form.deviceOrientation" :readonly="optType === 'view'"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="说明" prop="shuoming">
          <el-input
            v-model="form.shuoming"
            type="textarea"
            placeholder="请输入内容"
            :readonly="optType === 'view'"
          />
        </el-form-item>-->

        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
            :readonly="optType === 'view'"
          />
        </el-form-item>
        <el-form-item label="合同" prop="fileUrl">
          <FileUpload
            :limit="100"
            :fileSize="500"
            :fileType="fileType"
            :value="fileList"
            :optType="optType"
            @onUploaded="handleUploaded"
            @onRemoved="handleRemoved">
          </FileUpload>
        </el-form-item>
      </el-form>

      <el-divider v-if="form.soId !=null && optType !== 'change'" content-position="center">产品信息</el-divider>
      <el-card shadow="always" v-if="form.soId != null && optType !== 'change'" class="box-card">
        <SoOrderLine :soId="form.soId" :optType="optType"></SoOrderLine>
      </el-card>
      </el-tab-pane>
      <el-tab-pane   label="审批记录" name="审批记录" v-if="optType!=='add' && this.form.instanceId" >
        <el-timeline id="approveTimeLine">
          <el-timeline-item
            v-for="item in approveFlow"
            :key="item.id"
            :timestamp="item.updateTime"
            placement="top"
            icon="el-icon-success"
            color="#13ce66"
          >
            <el-card>
              <h3>{{item.nodeName}}
                <b>{{item.approver}}</b>
                <span
                  style="font-weight:800;"
                  :style="{ color: item.skipType === 'PASS' ? 'green' : 'red' }">
                  {{item.skipType === 'PASS' ?'同意':'驳回'}}
                </span>
              </h3>
              <p style="padding-top: 20px;">审批意见：{{ item.message }}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-tab-pane>
        <el-tab-pane   label="审批图" name="审批图" v-if="optType!=='add' && this.form.instanceId" >
          <img :src="imgUrl" width="100%" style="margin:0 auto"/>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="optType !== 'view' && optType !== 'confirm' &&
            (form.status === '草稿' ||
            form.status === '已驳回' ||
            form.status === '已审批')"
        >确 定</el-button>
        <el-button
          type="primary"
          @click="confirm"
          v-if="optType === 'confirm' &&(form.status === '草稿' || form.status === '已驳回')">提交审批</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 历史记录 -->
    <el-dialog title="销售订单变更历史记录" :visible.sync="historyVisible" width="80%" class="history-dialog">
      <Record :soId="soId" ref="record"></Record>
    </el-dialog>

    <!-- 变更列表 -->
    <el-dialog title="销售订单变更" :visible.sync="changeVisible" width="80%" class="change-dialog">
      <SoOrderLine :soId="form.soId" :optType="optType"></SoOrderLine>
    </el-dialog>

  </div>
</template>

<script>
import {
  addOrder,
  approveFlow,
  confirmOrder,
  delOrder,
  getOrder, listApproveOrder,
  listOrder,
  passOrder,
  updateOrder
} from "@/api/mes/xs/order";
import {genCode} from "@/api/system/autocode/rule"
import ClientSelect from "@/components/clientSelect/single.vue";
import ProjectSelect from "@/components/projectSelect/single.vue";
import UserSelect from "@/components/userSelect/single.vue";
import SoOrderLine from "./line.vue";
import FileUpload from "@/components/FileUpload/index";
import Record from "./record.vue";

export default {
  name: "Order",
  components: {ClientSelect, SoOrderLine, UserSelect, FileUpload, Record , ProjectSelect},
  data() {
    return {
      imgUrl: '',
      approveFlow:[{}],
      activeTab:'订单信息',
      //自动生成编码
      autoGenFlag:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      soId:null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单表格数据
      orderList: [],
      // 销售单产品行表格数据
      xsOrderLineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNumber: null,
        soName: null,
        clientName: null,
        orderDate: null,
        status: null,
        params: [],
      },
      // 表单参数
      form: {},
      // 查看开关
      optType: undefined,
      // 上传文件类型
      fileType: ["pdf", "doc", 'docx', "xls", 'xlsx', "jpg", "png"],
      // 上传文件列表
      fileList: [],
      // 表单校验
      rules: {
        soNumber: [
          { required: true, message: "销售订单编号不能为空", trigger: "blur" },
        ],
        clientCode: [
          { required: true, message: "请指定客户", trigger: "blur" }
        ],
        orderDate: [
          { required: true, message: "请选择订单日期", trigger: "change" }
        ],
        deliveryDate: [
          { required: true, message: "请选择交货日期", trigger: "change" }
        ],
        salesPerson: [
          { required: true, message: "销售人员不能为空", trigger: "blur" }
        ]
      },
      historyVisible: false,
      changeVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售订单列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listOrder(query).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        soId: null,
        soNumber: null,
        soName: null,
        clientId: null,
        clientCode: null,
        clientName: null,
        productId: null,
        productCode: null,
        productName: null,
        orderDate: null,
        deliveryDate: null,
        salesPerson: null,
        status: "草稿",
        fileUrl: null,
        remarks: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: null,
        paintingRequirements: null,
        deviceOrientation: null,
        deviceCode: null,
        shuoming: null,
        instanceId: null,
      };
      this.autoGenFlag = false;
      this.xsOrderLineList = [];
      this.fileList = [];
      this.resetForm("form");
      this.activeTab='订单信息'
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.soId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售订单";
      this.optType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const soId = row.soId || this.ids
      getOrder(soId).then(response => {
        this.form = response.data;
        this.xsOrderLineList = response.data.xsOrderLineList;
        response.data.fileUrl === null ? this.fileList = [] : this.fileList = response.data.fileUrl.split(',');
        this.open = true;
        this.title = "修改销售订单";
        this.optType = "edit";
        if (this.form.instanceId){
          approveFlow(this.form.instanceId).then(re => {
            this.approveFlow = re.data.map(item => {
              const updateTime = item.updateTime;
              if (updateTime) {
                const date = new Date(updateTime);
                if (!isNaN(date.getTime())) {
                  item.updateTime = date.toISOString().slice(0, 19).replace('T', ' ');
                } else {
                  item.updateTime = '';
                }
              } else {
                item.updateTime = '';
              }
              return item;
            });
            listApproveOrder(this.form.instanceId).then(response => {
              this.imgUrl = "data:image/gif;base64," + response.data;
            });
          })
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.soId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const soIds = row.soId || this.ids;
      this.$modal.confirm('是否确认永久删除编号为 “' + row.soNumber + '” 的销售订单？ 删除后将不可找回！').then(function() {
        return delOrder(soIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/xs/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag){
      if(autoGenFlag){
        genCode('SALES_ORDER_CODE').then(response =>{
          this.form.soNumber = response;
        });
      }else{
        this.form.soNumber = null;
      }
    },
    handleSelectClient(){
      this.$refs.clientSelect.showFlag = true;
    },
    handleSelectProject(){
      this.$refs.projectSelect.showFlag = true;
    },
    //客户选择弹出框
    onClientSelected(obj){
      if(obj != undefined && obj != null){
        this.form.clientId = obj.clientId;
        this.form.clientCode = obj.clientCode;
        this.form.clientName = obj.clientName;
      }
    },
    //项目选择弹出框
    onProjectSelected(obj){
      if(obj != undefined && obj != null){
        // 后端用产品id 产品编码 产品名称 字段 存 项目
        this.form.productId = obj.projectId;
        this.form.deviceCode = obj.projectCode;
        this.form.productName = obj.projectName;
      }
    },
    handleSelectUser(){
      this.$refs.userSelect.showFlag = true;
    },
    // 用户选择弹出框
    onUserSelected(obj){
      if(obj != undefined && obj != null){
        this.form.salesPerson = obj.nickName;
        this.form.deptId = obj.dept.deptId;
        this.form.deptName = obj.dept.deptName;
      }
    },
    // 审批
    handlePass(row) {
      const data = { soId: row.soId, status: '已审批' };
      this.$modal.confirm('是否审批通过编号为” ' + row.soNumber + ' ”的销售订单？ 审批通过后将不可撤回！').then(function() {
        return passOrder(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审批成功");
      }).catch(() => {});
    },
    //提交审批
    confirm(){
      let that = this;
      this.$modal.confirm('是否确认并提交审批编号为” ' + this.form.soNumber + ' ”的销售订单？').then(function() {
        confirmOrder(that.form.soId).then(response => {
          that.$modal.msgSuccess("操作成功");
          that.open = false;
          that.getList();
        })
      });
    },
    // 查询明细按钮操作
    handleConfirm(row){
      this.reset();
      const soId = row.soId
      getOrder(soId).then(response => {
        this.form = response.data;
        response.data.fileUrl === null ? this.fileList = [] : this.fileList = response.data.fileUrl.split(',');
        this.open = true;
        this.title="销售订单提交审批"
        this.optType = "confirm";
        if (this.form.instanceId){
          approveFlow(this.form.instanceId).then(re => {
            this.approveFlow = re.data.map(item => {
              const updateTime = item.updateTime;
              if (updateTime) {
                const date = new Date(updateTime);
                if (!isNaN(date.getTime())) {
                  item.updateTime = date.toISOString().slice(0, 19).replace('T', ' ');
                } else {
                  item.updateTime = '';
                }
              } else {
                item.updateTime = '';
              }
              return item;
            });
            listApproveOrder(this.form.instanceId).then(response => {
              this.imgUrl = "data:image/gif;base64," + response.data;
            });
          })
        }
      });
    },

    // 查询明细按钮操作
    handleView(row){
      this.reset();
      const soId = row.soId
      getOrder(soId).then(response => {
        this.form = response.data;
        response.data.fileUrl === null ? this.fileList = [] : this.fileList = response.data.fileUrl.split(',');
        this.open = true;
        this.title = "查看销售订单";
        this.optType = "view";
        if (this.form.instanceId){
          approveFlow(this.form.instanceId).then(re => {
            this.approveFlow = re.data.map(item => {
              const updateTime = item.updateTime;
              if (updateTime) {
                const date = new Date(updateTime);
                if (!isNaN(date.getTime())) {
                  item.updateTime = date.toISOString().slice(0, 19).replace('T', ' ');
                } else {
                  item.updateTime = '';
                }
              } else {
                item.updateTime = '';
              }
              return item;
            });
            listApproveOrder(this.form.instanceId).then(response => {
              this.imgUrl = "data:image/gif;base64," + response.data;
            });
          })
        }
      });
    },
    formatCurrency(number) {
      return new Intl.NumberFormat('zh-CN', { style: 'currency', currency: 'CNY' }).format(number);
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.fileUrl = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.fileUrl = data;
    },
    // 变更
    handleChange(row) {
      this.reset();
      this.form.soId = row.soId;
      this.changeVisible = true;
      this.optType = "change";
    },
    //历史记录
    handleRecord(row) {
      this.soId = null;
      this.soId = row.soId;
      this.historyVisible = true;
      this.$nextTick(() => {
        this.$refs.record.getList();
      })
    }
  }
};
</script>
<style >
#approveTimeLine .el-timeline-item__node--normal{
  width: 18px;
  height: 18px;
  left: -4px;
}
</style>
